package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController {

    @DubboReference
    private HouseService houseService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private UserFellowService userFellowService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    @ResponseBody
    public Result<PageInfo<HouseVo>> findListPage(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody HouseQueryVo houseQueryVo
    ){

        //调用服务层方法，待条件查询分页信息
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum,pageSize,houseQueryVo);

        //将PageInfo对象放入Result返回
        return Result.ok(pageInfo);
    }

    @RequestMapping("/info/{id}")
    @ResponseBody
    public Result info(@PathVariable("id") Long id, HttpServletRequest request){
        //房屋信息
        House house = houseService.getById(id);
        //小区信息
        Community community = communityService.getById(house.getCommunityId());
        //所欲经纪人
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        //房源图片
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
       //关闭房源 暂时写死
        //boolean isFollow = false;
        Map<String,Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);
        //关注业务后续补充，当前默认返回未关注
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        Boolean isFollow = false;
        if(null != userInfo){
            Long userId = userInfo.getId();
            isFollow = userFellowService.isFollowed(userId,id);
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);

    }



















}
